package com.sise.netty.基于jboss的序列化方案.serialHandler;

import io.netty.handler.codec.marshalling.MarshallingDecoder;
import io.netty.handler.codec.marshalling.UnmarshallerProvider;
import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallingEncoder;
import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallerProvider;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;

/**
 * 基于Marshalling的编码处理器
 *
 * @author idea
 * @data 2019/4/19
 */
public class MarshallingCodeHandler {

    public static MarshallingDecoder buildMarshallingDecoder() {
        //构建一个解码工厂
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        //设置配置类
        final MarshallingConfiguration configuration = new MarshallingConfiguration();

        //注意：如果jdk版本在5以上的话，这里需要定义为5
        configuration.setVersion(5);

        UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);

        //定义传输的数据对象大小
        MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024 * 1024 * 1);
        return decoder;
    }

    public static MarshallingEncoder buildMarshallingEncoder() {
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
        //构建netty的encoder对象，实现序列化接口的方式
        MarshallingEncoder encoder = new MarshallingEncoder(provider);
        return encoder;
    }
}
